package com.jss.lanouhn.fristapp.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jss.lanouhn.fristapp.R;
import com.jss.lanouhn.fristapp.modles.SelecterCommendInfo;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by lanouhn on 16/9/5.
 */
public class RvcAdapter extends RecyclerView.Adapter<RvcAdapter.CommentViewHolder>{
    private SelecterCommendInfo selecterCommendInfo;
    private Context context;

    public RvcAdapter(SelecterCommendInfo selecterCommendInfo, Context context) {
        this.selecterCommendInfo = selecterCommendInfo;
        this.context = context;
    }

    @Override
    public CommentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_rv_comment,null);
        CommentViewHolder holder=new CommentViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(CommentViewHolder holder, int position) {
        SelecterCommendInfo.DataBean.CommentsBean bean=
                selecterCommendInfo.getData().getComments().get(position);

        Picasso.with(context).load(bean.getUser().getAvatar_url()).into(holder.cir);
        holder.tv_name.setText(bean.getUser().getNickname());
        holder.tv_time.setText(new SimpleDateFormat("yyyy-MM-dd  hh:mm").format(new Date(bean.getCreated_at()*1000L)) );
        if (bean.getReply_to_id()!=null){
            holder.tv_replyname.setVisibility(View.VISIBLE);
            holder.tv_replyname.setText("回复"+bean.getReplied_user().getNickname()+": ");
            holder.tv_comment.setText(bean.getContent());
        }else{
            holder.tv_comment.setText(bean.getContent());

        }


    }

    @Override
    public int getItemCount() {

        return selecterCommendInfo.getData().getComments().size();
    }

    public class CommentViewHolder extends RecyclerView.ViewHolder{
        private CircleImageView cir;
        private TextView tv_name,tv_time,tv_replyname,tv_comment;
        public CommentViewHolder(View itemView) {
            super(itemView);
            cir= (CircleImageView) itemView.findViewById(R.id.cir_comments);
            tv_name= (TextView) itemView.findViewById(R.id.tv_comment_name);
            tv_time= (TextView) itemView.findViewById(R.id.tv_comment_times);
            tv_replyname= (TextView) itemView.findViewById(R.id.tv_reply_name);
            tv_comment= (TextView) itemView.findViewById(R.id.tv_comment);
        }

    }
}
